extends TextureRect

var recipe_data: Resource
@onready var main_bar = get_tree().current_scene
var sfx_pull = preload("res://sound/bar/mixwine_pull.wav")
var sfx_serve = preload("res://sound/bar/mixwine_serve.wav")# 初始化函数

func setup(data: Resource):
	recipe_data = data
	texture = data.result_icon

#核心：拖拽逻辑
func _get_drag_data(at_position):
	if not Global.can_interact: return null
	get_tree().current_scene.play_sfx(sfx_serve)
	#创建预览图
	var preview = TextureRect.new()
	preview.texture = texture
	
	preview.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
	preview.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
	preview.size = Vector2(100, 100) 
	preview.modulate.a = 1.0 
	
	#修正鼠标抓取位置
	var preview_control = Control.new()
	preview_control.add_child(preview)
	preview.position = -preview.size / 2 
	set_drag_preview(preview_control)
	
	#把本体变透明
	self.modulate.a = 0.0 
	
	# 返回数据
	return { "type": "drink", "recipe": recipe_data, "node": self }

#防丢失逻辑
#当拖拽结束时 (无论成功还是失败)，Godot 都会发送这个通知
func _notification(what):
	if what == NOTIFICATION_DRAG_END:
		#is_drag_successful()会告诉你刚才那次拖拽有没有被别人接收
		if not is_drag_successful():
			#如果没人接收 (拖到了空地上)，就把本体显示回来
			#在 0.2秒内做一个渐变动画，看起来更平滑
			var tween = create_tween()
			tween.tween_property(self, "modulate:a", 1.0, 0.2)
		else:
			#如果成功了(给客人了或倒掉了)，接收方(Customer/Bin)会负责 queue_free()销毁它
			#所以这里不用管
			pass

#右键倒酒逻辑
func _gui_input(event):
	if event is InputEventMouseButton and event.pressed:
		if event.button_index == MOUSE_BUTTON_RIGHT:
			if not Global.can_interact: return
			main_bar.play_sfx(sfx_pull)
			#飘字提示
			main_bar.show_floating_text("倒掉了...", Color.GRAY, global_position)
			
			#销毁自己
			queue_free()
			accept_event()

func cancel_drop():
	#做一个快速的渐变动画，让它浮现出来
	var tween = create_tween()
	tween.tween_property(self, "modulate:a", 1.0, 0.2)
